package org.higginsexamples.multiusermail.rcpmail;

import org.eclipse.higgins.rp.icard.token.identity.DigitalIdentity;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.layout.LayoutConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.higginsexamples.multiusermail.hbxclient.HBXClientHelper;

public class LoginDialog extends TitleAreaDialog {

	private static RcpMailModel model;

	private Text email;
	private Text password;
	private DigitalIdentity iCard;

	private Image iCardImage;

	public LoginDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("RCP Mail Login");
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		// init statics
		model = null;

		setTitle("Please select an iCard to login");
		// TODO nice message
		setMessage("This is the message....", IMessageProvider.INFORMATION);

		Composite dialogArea = (Composite) super.createDialogArea(parent);
		Composite contents = new Composite(dialogArea, SWT.NONE);

		contents.setLayout(new GridLayout(2, false));
		contents.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		new Label(contents, 0).setText("E-Mail:");
		email = new Text(contents, SWT.BORDER);

		new Label(contents, 0).setText("Password:");
		password = new Text(contents, SWT.BORDER | SWT.PASSWORD);

		new Label(contents, 0).setText("Select iCard");
		Button selectCardButton = new Button(contents, SWT.NONE);
		iCardImage = Activator.getImageDescriptor("/icons/icard_small.gif")
				.createImage();
		selectCardButton.setImage(iCardImage);
		selectCardButton.setLayoutData(new GridData(SWT.LEFT, SWT.NONE, false,
				false));
		selectCardButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO select the card here, put it into a field, will be used
				// by okPressed.
				try {
					iCard = new HBXClientHelper().callHBXClient();
					System.out.println(iCard);
					System.out.println("TODO: Card selection for "
							+ email.getText() + " with password "
							+ password.getText());
					model = RcpMailModel.login(email.getText(), password
							.getText(), iCard);
					setReturnCode((model != null) ? OK : CANCEL);
					close();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					setReturnCode(CANCEL);
					close();

				}
			}
		});

		Point defaultMargins = LayoutConstants.getMargins();
		GridLayoutFactory.fillDefaults().numColumns(2).margins(
				defaultMargins.x, defaultMargins.y).generateLayout(contents);

		return dialogArea;
	}

	@Override
	protected void okPressed() {
		model = RcpMailModel.login(email.getText(), password.getText(), iCard);
		setReturnCode((model != null) ? OK : CANCEL);
		close();
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Login", true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	@Override
	public boolean close() {
		iCardImage.dispose();
		return super.close();
	}

	public static RcpMailModel getModel() {
		return model;
	}

}
